#!/usr/local/bin/perl58 -w
#use strict;
#use DBI;

#0,15,30,45 * * * * /export/home/root/cron/query_drive_status.pl >> /export/home/root/cron/output/error 2>&1


#to make older solaris perl dbi client connect w/ newer mysql servers,
#run this on mysql server:
#set password for 'nbuadmin'@'%' = OLD_PASSWORD('asdfjk');

#	my $db="netbackup";
#	my $table="drive_status";
#	my $host="ilwrath";
#	my $user="nbuadmin";
#	my $password="asdfjk";

#	my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host",$user,$password) or die "Can't connect to database: $DBI::errstr\n";
#	my $sth=$dbh->prepare('insert into drive_status (drive_index,status) values (?,?)');


my @vmoprcmd_ds=`/opt/openv/volmgr/bin/vmoprcmd -d ds|tail +5|awk '{print \$1,\$3,\$8}'`;
my $section;
#my $timestamp=`/bin/date +'%Y-%m-%d %H:%M:%S'`;
#chomp($timestamp);

my $timestamp=int(time);


#$timestamp=~/.+ (\d+):/;
#if($1 eq '00'){
#	open(OUTPUT,">output/query_drive_status.output");
#}else{
	open(OUTPUT,">>/export/home/root/cron/output/query_drive_status.output");
#}

foreach my $line (@vmoprcmd_ds){
	my @items=split(/\s/,$line);

	my $drive_index=$items[0];
	my $status;

	if(($items[1] eq 'TLD') && (!defined($items[2]))){
#idle
		$status="0"; 
	}else{
#busy
		$status="1";
	}
	if($items[1] eq 'DOWN-TLD'){
#down
		$status="-1";
	}

	print OUTPUT "$drive_index^$status^$timestamp\n";
#	$sth->execute($drive_index,$status);

}
